const { sequelize } = require('../utils/sequelize');
const StudentCourseService = require('./StudentCourseService');

class GradeService {
    constructor() {
    }

    async findById(academyId){
        let data = null;
        data = await sequelize.models.Academy.findOne({
            where: {academy_id: academyId}});
        
        return data;
    }

    async saveGrade(obj){
        let data = null;
        data = await sequelize.transaction(function(t) {
            return sequelize.models.Grade.create({
                courseId: obj.courseId,
                studentId: obj.studentId,
                grade: obj.grade
            }, {transaction: t}).then(result => {
                return sequelize.models.StudentCourse.update({ status: 1},{
                    where: {
                        courseId: obj.courseId,
                        studentId: obj.studentId
                    }
                })
            })
        })

        return data;
    }

    async updateGade(obj){
        let data = null;
        let gradeObj = {
            grade: obj.grade
        }
        data = await sequelize.models.Grade.update({...gradeObj}, {
            where: {id: obj.id}
        })
        return data;
    }
}

module.exports = new GradeService();